package 学习计划.数据结构;

/**
 * @author 会玩的洋洋
 * https://leetcode.cn/problems/add-strings/?envType=study-plan&id=shu-ju-jie-gou-ji-chu&plan=data-structures&plan_progress=cqjfoh6
 */
public class _415_字符串相加 {
    /**
     * 执行用时：1ms，内存消耗：41.8MB
     * @param num1
     * @param num2
     * @return
     */
    public String addStrings(String num1, String num2) {
        int n = num1.length() - 1;
        int m = num2.length() - 1;
        int add = 0;
        StringBuffer sb = new StringBuffer();
        while (n >= 0 || m >= 0 || add != 0) {
            int x = n >= 0 ? num1.charAt(n) - '0' : 0;
            int y = m >= 0 ? num2.charAt(m) - '0' : 0;
            int sum = x + y + add;
            sb.append(sum % 10);
            add = sum / 10;
            n--;
            m--;
        }
        sb.reverse();
        return sb.toString();
    }
}
